package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateEnvironment
//@description: 创建Environment记录
//@param: environment model.Environment
//@return: err error

func CreateEnvironment(environment model.Environment) (err error) {
	err = global.GVA_DB.Create(&environment).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteEnvironment
//@description: 删除Environment记录
//@param: environment model.Environment
//@return: err error

func DeleteEnvironment(environment model.Environment) (err error) {
	err = global.GVA_DB.Delete(environment).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteEnvironmentByIds
//@description: 批量删除Environment记录
//@param: ids request.IdsReq
//@return: err error

func DeleteEnvironmentByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.Environment{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateEnvironment
//@description: 更新Environment记录
//@param: environment *model.Environment
//@return: err error

func UpdateEnvironment(environment model.Environment) (err error) {
	err = global.GVA_DB.Save(&environment).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetEnvironment
//@description: 根据id获取Environment记录
//@param: id uint
//@return: err error, environment model.Environment

func GetEnvironment(id uint) (err error, environment model.Environment) {
	err = global.GVA_DB.Where("id = ?", id).First(&environment).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetEnvironmentInfoList
//@description: 分页获取Environment记录
//@param: info request.EnvironmentSearch
//@return: err error, list interface{}, total int64

func GetEnvironmentInfoList(info request.EnvironmentSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.Environment{})
    var environments []model.Environment
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Name != "" {
        db = db.Where("`name` LIKE ?","%"+ info.Name+"%")
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&environments).Error
	return err, environments, total
}